define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prl/fields/index',
                    add_url: 'prl/fields/add/table_type/' + Config.type + '/table_id/' + Config.ids,
                    edit_url: 'prl/fields/edit',
                    del_url: 'prl/fields/del',
                    multi_url: 'prl/fields/multi',
                    table: 'prl_fields',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'title', title: __('Title')},
                        {field: 'submissionswitch', title: __('Submissionswitch'), formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
                ,queryParams : function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if(Config.type != null && Config.ids != null){
                        filter.table_type = Config.type;
                        filter.table_id = Config.ids;
                        op.table_type = "=";
                        op.table_id = "=";
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $(document).on("change", "form select[name='row[type]']", function (e) {
                    var value = $(this).val();
                    $("#c-content").parents('.form-group')
                    .toggleClass("hide", ['select', 'selects', 'checkbox', 'radio'].indexOf(value) > -1  ? false : true);
                    $("#c-length").parents('.form-group')
                    .toggleClass("hide", ['string','text', 'editor'].indexOf(value) > -1  ? false : true);
                    $("#c-max_select").parents('.form-group')
                    .toggleClass("hide", ['selects','checkbox'].indexOf(value) > -1  ? false : true);
                    $("#c-min_select").parents('.form-group')
                    .toggleClass("hide", ['selects','checkbox'].indexOf(value) > -1  ? false : true);
                });

                Form.api.bindevent($("form[role=form]"));
                //触发选中
                $("form select[name='row[type]']").trigger("change");
            }
        },
        lists : function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prl/fields/lists/type/' + Config.type + '/ids/' + Config.ids,
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: ''
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    Config.field
                ]
                // ,queryParams : function (params) {
                //     var filter = JSON.parse(params.filter);
                //     var op = JSON.parse(params.op);
                //     if(Config.type != null && Config.ids != null){
                //         filter.table_type = Config.type;
                //         filter.table_id = Config.ids;
                //         op.table_type = "=";
                //         op.table_id = "=";
                //     }
                //     params.filter = JSON.stringify(filter);
                //     params.op = JSON.stringify(op);
                //     return params;
                // },
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        }
    };
    return Controller;
});